#include <cassert>
#include <iostream>
#include <fstream>
#include <string>
#include <utility>
#include <map>
#include <cctype>
#include <vector>

std::pair<std::string, std::string> word_split(const char* raw)
{
	const char* it = raw;
	
	std::string first;
	
	// first is everything up until the first space
	for ( ; *it != 0 ; ++it) {
		if (*it == '(') {
			first = std::string(raw, it);
			
			// and now let's eat until we got out of the the paren
			for (; *it != 0 ; ++it) {
				if (*it == ')')
					break;
			}
			
			break;
		} else if (::isspace(*it)) {
			first = std::string(raw, it);
			break;
		}
	}
	
	const char* second_start = it;
	
	// eat all leading spaces
	for ( ; *it != 0 ; ++it) {
		if (::isspace(*it)) {
			second_start = it;
		} else {
			break;
		}
	}
	
	// and find the end
	for (; *it != 0 ; ++it) {}
	
	std::string second(second_start, it);
	
	return std::make_pair(first, second);
}


std::stringstream & print_case(std::stringstream & stream, const char letter)
{
	stream << "case '" << letter << "': ";
	
	return stream;
}

std::stringstream & print_end(std::stringstream & stream, std::string const& pronunciation)
{
	stream << "case '\0': return " << pronunciation << ";";
	return stream;
}

std::stringstream & print_switch(std::stringstream & stream, const int index)
{
	stream << "switch(v[" << index << "]) { ";
	return stream;
}

std::stringstream & print_end_switch(std::stringstream & stream)
{
	stream << "default: return \"\"; } "
	return stream;
}


int string_same(std::string const& first, std::string const& second)
{
	int same = 0;

	for (int i = 0; i < std::min(first.size(), second.size()); ++i)
	{
		if (first[i] != second[i])
			same = i;
		else
			break;
	}
	
	return same;
}


int main(int argc, char** argv)
{
	if (argc != 3) {
		std::cout << "Errror. Usage ./create_words <input> <output>. E.g. ./create_words cmuDict.txt words.hpp";
		return -1;
	}

	std::ifstream in(argv[1]);

	std::multimap<std::string, std::string> data;

	char current_line[1024];
	
	while (true)
	{
		in.getline(current_line, sizeof current_line);
		
		if (in.gcount() == 0)
			break;
		else if (in.gcount() >= sizeof current_line - 1)
			assert(!"Why was a line so big? o.0");
		
		
		data.insert(word_split(current_line));
		 // std::pair<std::string, std::string> words(word_split(current_line));
		
	//	std::cout << "Found word: " << words.first << " with pronounciation of: " << words.second << "\n";
	}
	
	std::cout << "Inserted " << data.size() << " words" << std::endl;
	
	
	std::string last;
	
	std::stringstream out;
	
	print_switch(out, 0);
	
	for (std::multimap<std::string, std::string>::const_iterator it = data.begin() ; it != data.end() ; ++it)
	{
		std::string english(it->first);
		std::string pronounciation(it->second);
		
		const int same = string_same(last, english);
		const int need_to_undo = last.empty() ? 0 : (last.size() - same + 1); // will need to also undo the '\0'
		
		
		for (int i = 0; i < need_to_undo ; ++i) {
			print_end_switch();
		}
		
		print_case(out, english[english.size() - need_to_undo]);
		
		for (int i = english.size() - need_to_undoneed_to_undo; i < english.size() ; ++i) {
			print_
		} 
		
	}
	
	return 0;
}



}
